<template>
	<view class="ys">
		<view class="box">
			<view class="title">
				<text>房间信息</text>
			</view>
			<view class="name">
				房间名 : {{room.name || ''}}
			</view>
			<view class="name">
				开始时间 : {{info.create_time || ''}}
			</view>
			<view class="name" v-if="info.finish_time">
				完成时间 : {{info.finish_time || ''}}
			</view>
			<view class="name">
				{{roleConf.leader }} : {{leader.name || ''}}
			</view>
		</view>
		<view class="box" v-if="code">
			<view class="title">
				<text>房间二维码</text>
			</view>
			<view class="codeimg">
				<!-- <view class="nocode" v-if="code == ''" @click="getCode">
					<view class="icon qr"></view>
					<text>点击生成二维码</text>
				</view> -->
				<image :src="code" mode="widthFix" @click="showImg(code)"></image>
			</view>
		</view>
		<view class="box">
			<view class="title">
				<text>{{roleConf.staff}}列表</text>
			</view>
			<view class="name">
				<view class="no-list ft" v-if="staff.length == 0">
					- 暂无人员信息 -
				</view>
				<view class="item" v-for="(item,index) in staff" :key="index">
					<view class="imgbox">
						<image :src="item.staff.avatar_path || siteConf.logo" mode=""></image>
					</view>
					<view class="info">
						<view class="username">
							{{item.staff.name}}
						</view>
						<view class="phone">
							{{item.staff.phone || '未填'}}
						</view>
						<view class="time">
							{{item.create_time}}
						</view>
					</view>
					<view class="btn-group">
						<button class="red" :disabled="loading" :loading="loading" v-if="item.show_refund"
							@click="refundOrder(item,index)">退款</button>
						<button :disabled="loading" :loading="loading" v-if="removeBtn"
							@click="removeStaff(item,index)">移除</button>
					</view>
				</view>

			</view>
		</view>
		<view class="btns">
			<button class="btn" v-if="showFinishBtn" @click="finishOrder" :loading="loading" :disabled="loading">
				完成订单
			</button>
			<button class="btn" v-if="showJoinBtn && !isInRoom && !payTicket" @click="addOrder" :loading="loading"
				:disabled="loading">
				加入房间
			</button>
			<button class="btn" v-else-if="showJoinBtn && !isInRoom && payTicket" @click="addOrder" :loading="loading"
				:disabled="loading">
				缴纳台票
			</button>
		</view>
	</view>
</template>

<script>
	import {
		getRoomDetail,
		addOrder,
		finishOrder,
		removeStaff,
		refundStoreOrder
	} from "@/api/staff.js"
	export default {
		data() {
			return {
				loading: false,
				leader: [],
				room: [],
				staff: [],
				info: [],
				id: '',
				store_order_id: '',
				isInRoom: false,
				showJoinBtn: false,
				showFinishBtn: false,
				removeBtn: false,
				payTicket: false, //是否需要交台票
				code: ''
			}
		},
		onLoad(e) {
			if (e.id) {
				this.id = e.id
				this.getRoomDetail({
					room_id: this.id
				})
			} else if (e.scene) {
				//扫码进入
				let scene = decodeURIComponent(e.scene)
				let param = this.getUrlParams(scene)
				if (param.room_id) {
					this.id = param.room_id
					this.getRoomDetail({
						room_id: this.id
					})
				} else {
					return uni.$showMsg('房间错误')
				}
			} else if (e.store_order_id) {
				this.store_order_id = e.store_order_id
				this.getRoomDetail({
					store_order_id: this.store_order_id
				})
			}
		},
		methods: {
			showImg(code) {
				uni.previewImage({
					urls: [code]
				})
			},
			finishOrder() {
				var that = this
				uni.showModal({
					title: '温馨提示',
					content: '确定结束房间吗?',
					success(res) {
						if (res.confirm) {
							that.loading = true
							finishOrder({
								order_id: that.info.id
							}).then(res => {
								uni.$showMsg(res.msg)
								if (res.code == 0) {
									uni.$showMsg(res.msg, function() {
										uni.navigateBack()
									})
								}

							})
						}
					}
				})

			},
			addOrder() {
				var that = this
				uni.showModal({
					title: '温馨提示',
					content: '确定使用此房间吗?',
					success(res) {
						if (res.confirm) {
							that.loading = true
							addOrder({
								room_id: that.id
							}).then(res => {
								uni.$showMsg(res.msg)
								if (res.code == 0) {
									if (res.data.unifiedorder) {
										//拉起支付
										let paymentData = res.data.unifiedorder
										uni.requestPayment({
											timeStamp: paymentData.timeStamp,
											nonceStr: paymentData.nonceStr,
											package: "prepay_id=" + paymentData.prepay_id,
											signType: 'MD5',
											paySign: paymentData.paySign,
											success: function(result) {
												uni.$showMsg(result.message)
												that.getRoomList()
											},
											fail: function(err) {
												uni.$showMsg('取消支付了')
												that.loading = false
											}
										})
									} else {
										//直接加入房间
										that.loading = false
										that.getRoomList()
									}
								} else {
									that.loading = false
								}
							})
						}
					}
				})
			},

			getRoomDetail(options) {
				getRoomDetail(options).then(res => {
					if (res.code == 0) {
						this.info = res.data.order
						this.leader = this.info.leader
						this.room = this.info.room
						this.staff = this.info.staff
						this.isInRoom = res.data.isInRoom
						this.showJoinBtn = res.data.showJoinBtn
						this.showFinishBtn = res.data.showFinishBtn
						this.removeBtn = res.data.removeBtn
						this.payTicket = res.data.payTicket
						this.code = res.data.qrcode
					} else {
						uni.$showMsg(res.msg)
					}
				})
			},
			removeStaff(item, index) {
				let that = this
				uni.showModal({
					title: '温馨提示',
					content: `确定要移除此${this.roleConf.staff}吗`,
					success(data) {
						if (data.confirm) {
							that.loading = true
							removeStaff({
								order_id: that.info.id,
								staff_id: item.staff_id
							}).then(res => {
								that.loading = false
								uni.$showMsg(res.msg)
								if (res.code == 0) {
									that.staff.splice(index, 1)
								}
							})
						}
					}
				})
			},
			refundOrder(item, index) {
				let that = this
				uni.showModal({
					title: '温馨提示',
					content: '确定要退款台票吗',
					success(data) {
						if (data.confirm) {
							that.loading = true
							refundStoreOrder({
								order_staff_id: item.id
							}).then(res => {
								uni.$showMsg(res.msg)
								that.loading = false
								if (res.code == 0) {
									item.show_refund = false
								}
							})
						}
					}

				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		padding-top: 10px;
		padding-bottom: 60px;
	}

	.btns {
		position: fixed;
		bottom: 0;
		width: 100%;
		max-width: 500px;
		left: 50%;
		transform: translate(-50%);
		padding: 10px 0;
		background: #fff;

		.btn {
			margin: 0 auto;
		}
	}

	.box {
		background: #fff;
		border-radius: 10px;
		width: 96%;
		margin: 10px auto;
		padding: 10px;
		box-sizing: border-box;

		.title {
			font-weight: bold;
			font-size: 17px;
			position: relative;
			padding-bottom: 10px;

			&::after {
				position: absolute;
				width: 40px;
				height: 5px;
				background: $uni-bg-color;
				content: '';
				left: 0;
				bottom: 0;
				border-radius: 20px;
			}
		}

		.codeimg {
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;
			min-height: 120px;
			padding-bottom: 10px;

			.nocode {
				display: flex;
				align-items: center;
				justify-content: center;
				flex-direction: column;
				font-size: 12px;
				color: #777;
			}

			.icon {
				margin-bottom: 5px;
				font-size: 60px;
			}

			image {
				width: 60%;
			}
		}

		.name {
			padding: 10px;
			font-size: 15px;
		}

		.item {
			background: #fff;
			border-radius: 6px;
			box-sizing: border-box;
			width: 100%;
			margin-bottom: 15px;
			display: flex;
			align-items: center;

			.imgbox {
				width: 60px;
				height: 60px;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.info {
				width: calc(100% - 60px);
				padding-left: 20px;
				box-sizing: border-box;

				.username {
					font-weight: bold;
					font-size: 14px;
					display: flex;
					align-items: center;

					text {
						margin-left: 10px;
						background: $uni-bg-color;
						font-size: 12px;
						color: #fff;
						padding: 2px 8px;
						border-radius: 4px;
					}
				}

				.phone {
					margin: 5px 0;
					font-size: 13px;
				}

				.time {
					color: #777;
					font-size: 12px;
				}
			}

			.btn-group {
				display: flex;
				justify-content: flex-end;
				flex-direction: column;
				width: 100px;

				button {
					font-size: 12px;
					margin: 3px 0;
					padding: 0;
					width: 60px;
					background: #b60000;
					color: #fff;
				}

				.red {
					background-color: orangered;
				}
			}
		}
	}
</style>